/*
 *  MouseJoint.cpp
 *  Squirrels
 *
 *  Created by Dmitry Fadeev on 9/22/11.
 *  Copyright 2011 Arello Mobile. All rights reserved.
 *
 */

#include <physics/MouseJoint.h>
#include <physics/ValueLoader.h>
#include <Box2D/Dynamics/Joints/b2MouseJoint.h>

namespace physics
{
	
	MouseJoint::MouseJoint(World *world) : Joint(world)
	{
		jointDef.reset(new b2MouseJointDef());
	}
	
	MouseJoint::MouseJoint(World *world, Node * node, Node * node2, const physics::Vector2 & target) : Joint(world)
	{
		jointDef.reset(new b2MouseJointDef());
		
		GetJointDef()->target = b2Vec2(target.x / 100.0f, target.y / 100.0f);
		bodiesInfo.bodyA  = node2;
		bodiesInfo.bodyB  = node;
	}
	
	MouseJoint::~MouseJoint()
	{
	}
	
	bool MouseJoint::on_node(xml::Provider *prov, const std::string &name)
	{
		if(name == "Target")
		{
			if(!ValueReader::Read(prov, GetJointDef()->target))
				return false;
			
			GetJointDef()->target.x /= 100.0f;
			GetJointDef()->target.y /= 100.0f;
			
			return true;
		}
		
		if(name == "MaxForce")
			return ValueReader::Read(prov, GetJointDef()->maxForce);
		
		if(name == "Frequency")
			return ValueReader::Read(prov, GetJointDef()->frequencyHz);
		
		if(name == "Damping")
			return ValueReader::Read(prov, GetJointDef()->dampingRatio);
		
		return Joint::on_node(prov, name);
	}
	
	void MouseJoint::SetTarget(const physics::Vector2 & newTarget)
	{
		static_cast<b2MouseJoint*>(b2joint)->SetTarget(b2Vec2(newTarget.x / 100.0f, newTarget.y / 100.0f));
	}
	
	physics::Vector2 MouseJoint::GetTarget() const
	{
		b2Vec2 target = static_cast<b2MouseJoint*>(b2joint)->GetTarget();
		
		return physics::Vector2(target.x * 100.0f, target.y * 100.0f);
	}
	
	void MouseJoint::SetMaxForce(float force)
	{
		static_cast<b2MouseJoint*>(b2joint)->SetMaxForce(force);
	}
	
	float MouseJoint::GetMaxForce() const
	{
		return static_cast<b2MouseJoint*>(b2joint)->GetMaxForce();
	}
}